﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseUI;
using BLL;
using Model;

namespace Thksoft.Module.ProjectDetails
{
    public partial class IncomeOne : BasePage
    {
        private QS_PROJECTDETAILBLL probll = new QS_PROJECTDETAILBLL();
        /// <summary>
        /// 判断是否首次加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //判断是否是只查询
                if (Request.QueryString["Select"] == "sel")
                {
                    //btnSum.Style.Add("display", "none");
                    btnOk.Style.Add("display", "none");
                    gvMonthPlan.Enabled = false;
                    mytr.Attributes.Add("style", "display:none");
                    btnDaochu.CssClass = "bt_css_s";
                }
                BindInit();
            }
        }

        /// <summary>
        /// 绑定加载数据
        /// </summary>
        public void BindInit()
        {
            var bll = new QS_PLAN_DETAIL1_BLL();
            List<QS_PLAN_DETAIL1> data = null;
            var applyId = Convert.ToInt32(Request.QueryString["ApplyId"]);
            var proId = Convert.ToInt32(Request.QueryString["ProId"]);

            var edit = Request.QueryString["Edit"] != "1" ? false : true;
            switch (Request.QueryString["State"])
            {
                case "1":
                    ////data = bll.FindRangeYear1(applyId);
                    break;
                case "2":
                    data = bll.FindRangeMonth1(applyId, proId);
                    break;
                case "3":
                    //data = bll.FindRangeWeek1(applyId, proId);
                    break;
                default:
                    break;
            }

            decimal sum = 0;
            sum = bll.FindSelectbyId("T1.C_VALUE6", "T1", Request.QueryString["State"], applyId);
            data.Add(new QS_PLAN_DETAIL1 { N_NUM = -1, N_AMOUNT = sum });
            gvMonthPlan.DataSource = data;
            gvMonthPlan.DataBind();
        }

        //确定
        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                var entitys = new List<QS_PLAN_DETAIL1>();
                foreach (GridViewRow row in gvMonthPlan.Rows)
                {
                    //判断属于最后一行剔除
                    if (row.RowIndex == gvMonthPlan.Rows.Count - 1)
                    {
                        continue;
                    }
                    var c1 = row.Cells[0].Controls[1] as TextBox;
                    var c2 = row.Cells[1].Controls[1] as DropDownList;
                    var c3 = row.Cells[2].Controls[1] as TextBox;
                    var c4 = row.Cells[3].Controls[1] as TextBox;
                    var c5 = row.Cells[4].Controls[1] as TextBox;
                    var c6 = row.Cells[5].Controls[1] as TextBox;
                    var c7 = row.Cells[6].Controls[1] as TextBox;
                    var entity = new QS_PLAN_DETAIL1();
                    if (c1.Text.Trim() == "")
                    {
                        MessageBox.Show(this, "请先完善第" + (row.RowIndex + 1) + "数据");
                        return;
                    }
                    entity.N_NUM = Convert.ToInt32(c1.Text.Trim());
                    entity.C_PRONAME = c2.SelectedValue.Trim();
                    if (c3.Text.Trim() == "")
                    {
                        entity.N_DOSAGE = 0;
                    }
                    else
                    {
                        entity.N_DOSAGE = Convert.ToInt32(c3.Text.Trim());
                    }
                    if (c4.Text.Trim() == "")
                    {
                        entity.N_PRICE = 0;
                    }
                    else
                    {
                        entity.N_PRICE = Convert.ToDecimal(c4.Text.Trim());
                    }
                    if (c5.Text.Trim() == "")
                    {
                        entity.N_AMOUNT = 0;
                    }
                    else
                    {
                        entity.N_AMOUNT = Convert.ToDecimal(c5.Text.Trim());
                    }
                    entity.C_BANK = c6.Text.Trim();
                    entity.C_REMARK = c7.Text.Trim();
                    entitys.Add(entity);
                }

                var bll = new QS_PLAN_DETAIL1_BLL();
                var applyId = Convert.ToInt32(Request.QueryString["ApplyId"]);
                var proId = Convert.ToInt32(Request.QueryString["ProId"]);
                switch (Request.QueryString["State"])
                {
                    case "1":
                        //bll.UpdateOrCreateRangeYear1(applyId, proId, entitys);
                        break;
                    case "2":
                        bll.UpdateOrCreateRangeMonth1(applyId, proId, entitys);
                        break;
                    case "3":
                        //bll.UpdateOrCreateRangeWeek1(applyId, proId, entitys);
                        break;
                    default:
                        break;
                }
                MessageBox.ResponseScript(this, "app1.ReLoad();");
            }
            catch (Exception)
            {
                MessageBox.Show(this, "提交失败");
            }

        }
        //求和
        protected void btnSum_Click(object sender, EventArgs e)
        {
            //Sum();
        }

        //设置样式
        protected void gvMonthPlan_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex == -1)
            {
                return;
            }

            #region 绑定下拉列表框数据
            //项目名称
            var proId = Convert.ToInt32(Request.QueryString["ProId"]);
            var where = string.Format(@" AND C_TABLETYPE = '{0}' AND N_NAMETYPE = {1} AND N_PRIVATE = {2} ", "T1", 1, proId);
            var dt4 = new QS_PlanDetail_DropLIstBLL().SelectPlanDropList(where);

            if (((DropDownList)e.Row.FindControl("FORM_ProjectName")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("FORM_ProjectName");
                ddl.Items.Clear();
                if (dt4 != null)
                {
                    for (int i = 0; i < dt4.Rows.Count; i++)
                    {
                        ddl.Items.Insert(0, new ListItem(dt4.Rows[i][1].ToString(), dt4.Rows[i][0].ToString()));
                    }
                }
                ddl.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }
            #endregion

            QS_PLAN_DETAIL1 _row = e.Row.DataItem as QS_PLAN_DETAIL1;
            var row = e.Row;
            if (_row.N_NUM == -1)//如果数据为Null则代表为合计行
            {
                row.Cells[0].Text = "";
                row.Cells[1].Text = "合计";
                row.Cells[2].Text = "";
                row.Cells[3].Text = "";
                row.Cells[4].Text = "";
                row.Cells[5].Text = "";
                row.Cells[6].Text = "";
                return;
            }
            if (_row.N_NUM == -2)
            {
                return;
            }
            var c1 = row.Cells[0].Controls[1] as TextBox;
            var c2 = row.Cells[1].Controls[1] as DropDownList;
            var c3 = row.Cells[2].Controls[1] as TextBox;
            var c4 = row.Cells[3].Controls[1] as TextBox;
            var c5 = row.Cells[4].Controls[1] as TextBox;
            var c6 = row.Cells[5].Controls[1] as TextBox;
            var c7 = row.Cells[6].Controls[1] as TextBox;
            c1.Text = _row.N_NUM.ToString();
            c2.Text = _row.C_PRONAME.ToString();
            if (_row.N_DOSAGE != null)
            {
                c3.Text = _row.N_DOSAGE.ToString();
            }
            if (_row.N_PRICE != null)
            {
                c4.Text = _row.N_PRICE.ToString();
            }

            c5.Text = _row.N_AMOUNT.ToString();
            c6.Text = _row.C_BANK.ToString();
            c7.Text = _row.C_REMARK.ToString();
        }
        #region 添加行
        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var dt = new List<QS_PLAN_DETAIL1>();
            decimal sum = 0;

            //递归把数据从页面上抓取
            foreach (GridViewRow row in gvMonthPlan.Rows)
            {
                //判断属于最后一行剔除
                if (row.RowIndex == gvMonthPlan.Rows.Count - 1)
                {
                    continue;
                }
                var c1 = row.Cells[0].Controls[1] as TextBox;
                var c2 = row.Cells[1].Controls[1] as DropDownList;
                var c3 = row.Cells[2].Controls[1] as TextBox;
                var c4 = row.Cells[3].Controls[1] as TextBox;
                var c5 = row.Cells[4].Controls[1] as TextBox;
                var c6 = row.Cells[5].Controls[1] as TextBox;
                var c7 = row.Cells[6].Controls[1] as TextBox;
                var entity = new QS_PLAN_DETAIL1();
                if (c1.Text.Trim() == "")
                {
                    MessageBox.Show(this, "请先完善第" + (row.RowIndex + 1) + "数据");
                    return;
                }
                entity.N_NUM = Convert.ToInt32(c1.Text.Trim());
                entity.C_PRONAME = c2.Text.Trim();
                if (c3.Text.Trim() == "")
                {
                    entity.N_DOSAGE = 0;
                }
                else
                {
                    entity.N_DOSAGE = Convert.ToInt32(c3.Text.Trim());
                }
                if (c4.Text.Trim() == "")
                {
                    entity.N_PRICE = 0;
                }
                else
                {
                    entity.N_PRICE = Convert.ToDecimal(c4.Text.Trim());
                }
                if (c5.Text.Trim() == "")
                {
                    entity.N_AMOUNT = 0;
                    sum += entity.N_AMOUNT;
                }
                else
                {
                    entity.N_AMOUNT = Convert.ToDecimal(c5.Text.Trim());
                }
                entity.C_BANK = c6.Text.Trim();
                entity.C_REMARK = c7.Text.Trim();
                dt.Add(entity);
            }
            dt.Add(new QS_PLAN_DETAIL1 { N_NUM = -2 });
            dt.Add(new QS_PLAN_DETAIL1 { N_NUM = -1, N_AMOUNT = sum });

            gvMonthPlan.DataSource = dt;
            gvMonthPlan.DataBind();

        }
        #endregion

        #region 删除行
        /// <summary>
        /// 删除行（通过行的索引删除,如果索引为空则删除最后一行）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {
            var dt = new List<QS_PLAN_DETAIL1>();
            //递归把数据从页面上抓取
            foreach (GridViewRow row in gvMonthPlan.Rows)
            {
                //判断属于最后一行剔除
                if (row.RowIndex == gvMonthPlan.Rows.Count - 1)
                {
                    continue;
                }
                var c1 = row.Cells[0].Controls[1] as TextBox;
                var c2 = row.Cells[1].Controls[1] as DropDownList;
                var c3 = row.Cells[2].Controls[1] as TextBox;
                var c4 = row.Cells[3].Controls[1] as TextBox;
                var c5 = row.Cells[4].Controls[1] as TextBox;
                var c6 = row.Cells[5].Controls[1] as TextBox;
                var c7 = row.Cells[6].Controls[1] as TextBox;
                var entity = new QS_PLAN_DETAIL1();
                if (c1.Text.Trim() == "")
                {
                    entity.N_NUM = 0;
                }
                else
                {
                    entity.N_NUM = Convert.ToInt32(c1.Text.Trim());
                }
                entity.C_PRONAME = c2.Text.Trim();
                if (c3.Text.Trim() == "")
                {
                    entity.N_DOSAGE = 0;
                }
                else
                {
                    entity.N_DOSAGE = Convert.ToInt32(c3.Text.Trim());
                }
                if (c4.Text.Trim() == "")
                {
                    entity.N_PRICE = 0;
                }
                else
                {
                    entity.N_PRICE = Convert.ToDecimal(c4.Text.Trim());
                }
                if (c5.Text.Trim() == "")
                {
                    entity.N_AMOUNT = 0;
                }
                else
                {
                    entity.N_AMOUNT = Convert.ToDecimal(c5.Text.Trim());
                }
                entity.C_BANK = c6.Text.Trim();
                entity.C_REMARK = c7.Text.Trim();
                dt.Add(entity);
            }
            dt.Add(new QS_PLAN_DETAIL1 { N_NUM = -1 });
            if (dt.Count <= 1)
            {
                return;
            }
            if (p_id.Value == "")
            {
                //删除最后一行
                dt.RemoveAt(dt.Count - 2);
            }
            else
            {
                if (Convert.ToInt32(p_id.Value) > 0 && Convert.ToInt32(p_id.Value) < dt.Count)
                {
                    dt.RemoveAt(Convert.ToInt32(p_id.Value) - 1);
                }

            }
            gvMonthPlan.DataSource = dt;
            gvMonthPlan.DataBind();
        }
        #endregion

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaochu_Click(object sender, EventArgs e)
        {
        }

    }
}